﻿using maxiaopeng.Help;
using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.Controllers
{
    public class RolesController : ApiController
    {
        // GET: api/Roles

        //模块
        [HttpGet]
        public IHttpActionResult GetAllModules()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var datalist = db.Modules.Where(p => p.ParentId == 0).ToList();
                var bb = datalist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();
                List<ModuleList> data = new List<ModuleList>();
                foreach (var item in bb)
                {
                    var list = db.Modules.Where(p => p.ParentId == item.Id).ToList();
                    var cc = list.Select(p => new Modules()
                    {
                        Id = p.Id,
                        Name = p.Name,
                        ParentId = p.ParentId,
                        Path = p.Path,
                        Weight = p.Weight,
                    }).ToList();
                    var aa = new ModuleList()
                    {
                        Id = item.Id,
                        Name = item.Name,
                        ParentId = item.ParentId,
                        Path = item.Path,
                        Weight = item.Weight,
                        children = cc
                    };
                    data.Add(aa);
                };
                return Json(new { data = data, code = 0 });
            }
        }
        [HttpPost]
        public IHttpActionResult GetMyModules([FromBody]Roles role)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var datalist = db.Rolemodules.Where(p => p.RoleId == role.Id).ToList();
                var list = new List<int>();
                foreach (var item in datalist)
                {
                    list.Add(item.ModuleId);
                }
                return Json(new { data = list, code = 0 });
            }
        }
        [HttpGet]
        public IHttpActionResult EditRM(string Id, string Value)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                int n = 0;
                int id = Convert.ToInt32(Id);
                if (Value != null)
                {
                    string[] strArray = Value.Split(','); //字符串转数组
                    List<int> abc = new List<int>();
                    foreach (var g in strArray)
                    {
                        abc.Add(Convert.ToInt32(g));
                    }
                    var datalist = db.Rolemodules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.Rolemodules.Remove(item);
                        n += db.SaveChanges();
                    }
                    foreach (var i in abc)
                    {
                        var dd = new Rolemodules()
                        {
                            RoleId = id,
                            ModuleId = i
                        };
                        db.Rolemodules.Add(dd);
                        n += db.SaveChanges();
                    }
                }
                else
                {
                    var datalist = db.Rolemodules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.Rolemodules.Remove(item);
                        n += db.SaveChanges();
                    }
                }
                if (n >= 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }

        [HttpPost]
        public IHttpActionResult Delete([FromBody]Roles user)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Roles.FirstOrDefault(p => p.Id == user.Id);
                db.Roles.Remove(list);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody]Roles add)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Roles.FirstOrDefault(p => p.Id == add.Id);
                list.Name = add.Name;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult addRoles([FromBody]Roles add)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                db.Roles.Add(add);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "添加成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "添加失败！" });
                }
            }
        }
        [HttpGet]
        public IHttpActionResult Roles(int page, int limit, string Name)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var dataList = db.Roles.AsEnumerable();
                var count = dataList.Count();
                if (!string.IsNullOrEmpty(Name))
                {
                    dataList = dataList.Where(p => p.Name.Contains(Name));
                }
                //筛选页面所有符合条件的数据
                var list = dataList.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                });
                return Json(new { count = count, data = list.OrderByDescending(p => p.Id).Skip((page - 1) * 10).Take(limit).ToList() });

            }
        }
    }
}
